package com.fbt.andon.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fbt.andon.domain.BoardStation;
import com.fbt.andon.domain.Line;
import com.fbt.andon.domain.StopLine;
import com.fbt.andon.domain.Worker;
import com.fbt.andon.service.impl.StopLineServiceImpl;
import com.linkwechat.common.annotation.Log;
import com.linkwechat.common.core.controller.BaseController;
import com.linkwechat.common.core.domain.AjaxResult;
import com.linkwechat.common.enums.BusinessType;
import com.linkwechat.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;


/**
 * 停线Controller
 *
 * @author fengtao
 * @date 2021-08-11
 */
@RestController
@RequestMapping("/stopLine")
@Api(description = "停线api")
public class StopLineController extends BaseController {
    @Autowired
    private StopLineServiceImpl stopLineService;


    @GetMapping("/query")
    @ApiOperation("查询该表所有信息")
    //@PreAuthorize(hasPermi = "mes:andon:query")
    public AjaxResult findlineList() {
//        startPage();
        List<StopLine> list = stopLineService.list(null);
        return AjaxResult.success("查询成功", list);
    }

    @PostMapping("/FindByTime")
    @ApiOperation("根据时间来查询停线信息")
    //@PreAuthorize(hasPermi = "mes:andon:query")
    public AjaxResult remove(String startTime, String endTime) {
        // 查询年龄在 20 ~ 30 岁之间的用户
        QueryWrapper<StopLine> wrapper = new QueryWrapper<>();
        wrapper.between("stop_line", startTime, endTime); // 区间
        return AjaxResult.success("查询成功", stopLineService.list(wrapper));// 查询结果数
    }

    //@PreAuthorize(hasPermi = "mes:andon:query")
    @PostMapping("/findByFactory/{factory}")
    @ApiOperation("模糊查询停线信息")
    //    参数factory工厂，area区域，groupName班组名
    public AjaxResult findByFactory(@PathVariable("factory") String factory) {
        System.out.println("==============" + factory);
        startPage();
        QueryWrapper<StopLine> wrapper = new QueryWrapper<>();
        wrapper.like("factory", factory);
        return AjaxResult.success("查询成功", stopLineService.list(wrapper));
    }

    //@PreAuthorize(hasPermi = "mes:andon:query")
    @PostMapping("/queryByParams")
    @ApiOperation("范围查找信息")
//    查找大于等于intervalTime的秒数
    public AjaxResult queryByIntervalTime(@RequestBody StopLine stopLine) {
        String intervalTime = String.valueOf(stopLine.getIntervalTime());
        startPage();
        if (intervalTime != null && stopLine.getFactory() != null) {
            QueryWrapper<StopLine> wrapper = new QueryWrapper<>();
            wrapper.ge("interval_time", stopLine.getIntervalTime());
            wrapper.like("factory", stopLine.getFactory());
            return AjaxResult.success("查询成功", stopLineService.list(wrapper));
        } else if (intervalTime != null) {
            QueryWrapper<StopLine> wrapper1 = new QueryWrapper<>();
            wrapper1.ge("interval_time", stopLine.getIntervalTime());
            return AjaxResult.success("查询成功", stopLineService.list(wrapper1));
        } else if (stopLine.getFactory() != null) {
            QueryWrapper<StopLine> wrapper2 = new QueryWrapper<>();
            wrapper2.like("factory", stopLine.getFactory());
            return AjaxResult.success("查询成功", stopLineService.list(wrapper2));
        }
        return AjaxResult.success("查询成功", null);
    }

    //@PreAuthorize(hasPermi = "mes:andon:query")
    @PostMapping("/queryByTime")
    @ApiOperation("时间查找信息")
//    查找大于等于intervalTime的秒数
    public AjaxResult queryByTime(@RequestBody StopLine stopLine) {
        System.out.println(stopLine.getIntervalTime());
        System.out.println(stopLine.getOpenLine());
        startPage();
        if (stopLine.getStopLine() != null && stopLine.getOpenLine() != null) {
            QueryWrapper<StopLine> wrapper = new QueryWrapper<>();
            wrapper.between("stop_line", stopLine.getStopLine(), stopLine.getOpenLine()); // 区间
            return AjaxResult.success("查询成功", stopLineService.list(wrapper));
        } else if (stopLine.getStopLine() != null) {
            QueryWrapper<StopLine> wrapper1 = new QueryWrapper<>();
            wrapper1.ge("stop_line", stopLine.getIntervalTime());
            return AjaxResult.success("查询成功", stopLineService.list(wrapper1));
        } else if (stopLine.getFactory() != null) {
            QueryWrapper<StopLine> wrapper2 = new QueryWrapper<>();
            wrapper2.ge("stop_line", stopLine.getOpenLine());
            return AjaxResult.success("查询成功", stopLineService.list(wrapper2));
        }
        return AjaxResult.success("查询成功", null);
    }


    @GetMapping("/select/{id}")
    @ApiOperation("根据id获取停线信息，参数id")
    //@PreAuthorize(hasPermi = "mes:andon:query")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success("获取成功", stopLineService.getById(id));
    }


    @PostMapping("/add")
    @ApiOperation("新增产线，参数stopLine")
    //@PreAuthorize(hasPermi = "mes:andon:add")
    public AjaxResult add(@RequestBody StopLine stopLine) {
        System.out.println(stopLine);
        return AjaxResult.success("新增成功", stopLineService.save(stopLine));
    }


    @PostMapping("/update")
    @ApiOperation("根据id修改产线，参数stopLine")
    //@PreAuthorize(hasPermi = "mes:andon:edit")
    public AjaxResult edit(@RequestBody StopLine stopLine) {
        return AjaxResult.success("修改成功", stopLineService.updateById(stopLine));
    }

    //@PreAuthorize(hasPermi = "mes:andon:query")
    @ApiOperation("根据id查询人员")
    @GetMapping(value = "/{id}")
    public AjaxResult getByid(@PathVariable("id") Integer id) {
        return AjaxResult.success("获取成功", stopLineService.getById(id));
    }


    @PostMapping("/batchRemove/{ids}")
    @ApiOperation("根据id批量删除产线，参数id")
    //@PreAuthorize(hasPermi = "mes:andon:remove")
    public AjaxResult remove(@PathVariable("ids") Long[] ids) {
        return AjaxResult.success("删除成功", stopLineService.removeByIds(Arrays.asList(ids)));
    }


    /**
     * 导出产线列表
     */
    //@PreAuthorize(hasPermi = "mes:andon:export")
    @Log(title = "产线", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StopLine stopLine) throws IOException {
        List<StopLine> list = stopLineService.list(null);
        ExcelUtil<StopLine> util = new ExcelUtil<StopLine>(StopLine.class);
      //  util.exportExcel(response, list, "stopLine");
    }

}
